package com.njit.springboot.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.njit.springboot.bean.RoncooUserLog;
import com.njit.springboot.cache.RoncooUserLogCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping(value = "/rest", method = RequestMethod.GET)
public class RestRoncooController {

    @Autowired
    private RoncooUserLogCache roncooUserLogCache;

    @RequestMapping(value = "/update")
    public RoncooUserLog update(@RequestBody JsonNode jsonNode) {
        System.out.println("jsonNode=" + jsonNode);
        RoncooUserLog bean = roncooUserLogCache.selectByUserName(jsonNode.get("userName").asText("测试"));
        if (bean == null) {
            bean = new RoncooUserLog();
        }
        bean.setUserName("测试");
        bean.setCreateTime(new Date());
        bean.setUserIp("192.168.157.134");
        roncooUserLogCache.updateByUserName(bean);
        return bean;
    }

    @RequestMapping(value = "/update/{userName}", method = RequestMethod.GET)
    public RoncooUserLog update2(@PathVariable(value = "userName") String userName) {
        RoncooUserLog bean = roncooUserLogCache.selectByUserName(userName);
        if (bean == null) {
            bean = new RoncooUserLog();
        }
        bean.setUserName("测试");
        bean.setCreateTime(new Date());
        bean.setUserIp("192.168.157.135");
        roncooUserLogCache.updateByUserName(bean);
        return bean;
    }
}

